package cn.gaily.servicebase.exceptionhandler;

import cn.gaily.commonutils.ExceptionUtil;
import cn.gaily.commonutils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    //全局异常处理
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public R error(Exception e){
        e.printStackTrace();
        return R.error().message("出现异常,执行了全局处理");
    }

    //特定异常处理
    @ResponseBody
    @ExceptionHandler(ArithmeticException.class)
    public R givenError(ArithmeticException e){
        e.printStackTrace();
        return R.error().message("出现异常,执行了特定异常处理");
    }

    //自定义异常处理
    @ResponseBody
    @ExceptionHandler(GuliException.class)
    public R guliError(GuliException e){
        e.printStackTrace();
        log.error(ExceptionUtil.getMessage(e));
        return R.error().code(e.getCode()).message(e.getMsg());
    }
}
